# -*- coding: utf-8 -*-
import numpy as np
from sklearn import datasets

digits = datasets.load_digits()

X = digits["data"]
y = digits["target"]

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split( X, y )

from sklearn.neighbors import KNeighborsClassifier


'''
超参数：在算法运行前需要决定的参数
也称为网格搜索
'''
best_score = 0.0
best_k = -1 # 测试 最优的 k
best_p = -1 # 搜索 明可夫斯基 距离
for k in range( 1, 11 ):
    for p in range( 1, 6 ):
        my_knn_clf = KNeighborsClassifier( n_neighbors = k, weights = "distance", p = p )
        my_knn_clf.fit( X_train, y_train )
        score = my_knn_clf.score( X_test, y_test )
        if score > best_score:
            best_score = score
            best_k = k
            best_p = p
        
print( "best_k = ", best_k )
print( "best_score = ", best_score )
print( "best_p = ", best_p )
'''
best_k =  3
best_score =  0.997777777778
best_p =  2
'''